//
//  HETWRecodViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/9.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HETWRecodViewController.h"
#import "HEDoctorInfoModel.h"
#import "HETWRecodCell.h"
#import "HEIMTWChatViewController.h"

@interface HETWRecodViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray <HEDoctorInfoModel *>* dataSourceArr;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, strong) GDTableView *tableView;

@end

@implementation HETWRecodViewController

- (NSMutableArray <HEDoctorInfoModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"图文问诊";
    
    _pageNum = 1;
    _pageSize = 10;
    self.view.backgroundColor = CLightGrayColor;
    
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    [self.view addSubview:self.tableView];
    
    [self loadData];
}

- (GDTableView *)tableView {
    if (!_tableView) {  
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

            weakSelf.isFromHeadRefresh = YES;
            weakSelf.pageNum = 1;
            [weakSelf loadData];
        }];
 
        [_tableView registerClass:[HETWRecodCell class] forCellReuseIdentifier: KHETWRecodCell];

    }
    return _tableView;
}
#pragma mark -

- (void)loadData {

  
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KgetUserAsk parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject]; 
        [self.dataSourceArr addObjectsFromArray:[HEDoctorInfoModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.count == 0) {
                [Dialog toastBottom:@"没有更多数据了~"];
            }
        }

    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HETWRecodCell *cell = [_tableView dequeueReusableCellWithIdentifier: KHETWRecodCell];
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHETWRecodCell cacheByIndexPath:indexPath configuration:^(HETWRecodCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEDoctorInfoModel *model = self.dataSourceArr[indexPath.row];
    
    HEIMKitInfo *doc = [HEIMKitInfo new];
    doc.infoId = model.user_id;
    doc.referenceId = model.inst_id;
    doc.showName = model.realname;
    doc.avatarUrlString = model.avatar;
    doc.avatarImage = IMAGE_NAMED(@"hmang");
    
    UserInfo *user = [[UserManager sharedUserManager] getUserInfo];
    HEIMKitInfo *owner = [HEIMKitInfo new];
    owner.infoId = user.id;
    owner.showName = user.realname;
    owner.avatarUrlString = user.avatar;
    owner.avatarImage = IMAGE_NAMED(@"defualt_icon");
    
    [[HEIMKit sharedKit] registeUserInfos: @[doc, owner]];
    
    HEIMTWChatViewController *vc = [HEIMTWChatViewController new];
    vc.docId = model.doc_id;
    vc.inst_id = model.inst_id;
    vc.docName = model.realname;
    vc.backBlock = ^{
        [self.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

 


@end
